const app = getApp()
Page({
  data: {
    backshow:false,
    id1:'',
    id2:"",
    time1:'',
    time2:'',
    image1path:'',
    image2path: '',
    tabArr: {
      curHdIndex: 2,
    },
    user_type:'',//账号类型
    headmaster:'hide',
    parent:'hide',
    schooldata:{},//学校数据
    classdata:{},//班级数据
    studentdata: {},//个人学生数据
    accountInfo: {},//账号数据
    isshow:'hide',
    isbind: false,
    loadingHidden: false,
    userInfo: {},//微信账号数据
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    preIndex: 0,//上一页
    currentIndex:0,//当前
    swiperError:0,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 800,
    circular: true,
    imgUrls: [
    ],
    recdata:{},//识别数据
    links: [
      '../user/user',
      '../user/user',
      '../user/user'
    ]
  },
  //扫码
  sweepcode: function (event) {
    wx.scanCode({
      success: (res) => {
        var code = res.result;
        if (code == "" || code == null || code==undefined){
          return;
        }
        wx.navigateTo({
          url: "/pages/sweepcode/sweepcode?code=" + code
        })
      },
      fail: (res) => {
      }
    })
  },
  //积分
  integral: function (event) {
    wx.navigateTo({
      url: '../integral/integral'
    })
  },
  //班图
  classimage: function (event) {
    wx.navigateTo({
      url: '../classcard/classcard'
    })
  },
  //正向
  integralrecord: function (event) {
    wx.navigateTo({
      url: '../integralrecord/integralrecord'
    })
  },

//课表
  timetable: function (event) {
    wx.navigateTo({
      url: '../timetable/timetable'
    })
  },
  //轮播图的切换事件
  swiperChange: function (e) {
    if (e.detail.source == "touch") {
      //当页面卡死的时候，current的值会变成0 
      if (e.detail.current == 0) {
        let swiperError = this.data.swiperError
        swiperError += 1
        this.setData({ swiperError: swiperError })
        if (swiperError >= 3) {
          this.setData({ currentIndex: this.data.preIndex });//，重置current为正确索引
          this.setData({ swiperError: 0 })
        }
      } else {//正常轮播时，记录正确页码索引
        this.setData({ preIndex: e.detail.current });
        //将开关重置为0
        this.setData({ swiperError: 0 })
      }
    }
  },
  //点击指示点切换
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  handleTab: function (e) {
    var dataId = e.currentTarget.id;
    var obj = {};
    obj.curHdIndex = dataId;
    this.setData({
      tabArr: obj
    })
  },
  //点击图片触发事件
  swipclick: function (e) {
    wx.navigateTo({
      url: '../found/found'
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //跳转
  ufacedetail: function (e) {
    var dataId = e.currentTarget.dataset.idx;
    wx.navigateTo({
      url: '../uface/detail?id=' + dataId
    })
  },
  onLoad: function () {
    var that=this;
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    if (getApp().globalData.bindInfo) {
      that.setData({
        isbind: true,
        user_type: getApp().globalData.bindInfo.user_type
      });
      that.getmessagelist();
      if (getApp().globalData.bindInfo.user_type==3){
         that.getreclist();//获取识别记录
         
      }else{
          that.getfoundlist();
      }
      if (getApp().globalData.bindInfo.user_type == 1) {
        that.setData({
          headmaster: 'show',
          parent: 'hide',
          isbind: app.globalData.isbind,
        });
      }
      if (getApp().globalData.bindInfo.user_type == 3) {
        that.setData({
          headmaster: 'hide',
          parent: 'show',
        });
      }
      that.setData({
        loadingHidden: true,
      });
    }
    if (!app.isbindBack) {
      app.isbindBack = function (result) {
        that.setData({
          loadingHidden: true,
        });
        if (result.code == 1) {
          wx.redirectTo({
            url: '../index/index'
          })
          getApp().globalData.bindInfo = result.data;
          that.getmessagelist();
         
          if (getApp().globalData.bindInfo.user_type == 3) {
            that.getreclist();//获取识别记录
          } else {
            that.getfoundlist();
          }
          if (getApp().globalData.bindInfo.user_type==1){
            that.setData({
              headmaster: 'show',
              parent: 'hide',
            });
          }
          if (getApp().globalData.bindInfo.user_type == 3) {
            that.setData({
              headmaster: 'hide',
              parent: 'show',
            });
          }
          getApp().globalData.isbind = true;
          that.setData({
            isbind: true,
            user_type: app.globalData.bindInfo.user_type
          });
        } else if (result.code == 0) {
          getApp().globalData.isbind = false;
          that.setData({
            isbind: false,
          });
          wx.redirectTo({
            url: '../binding/binding'
          })
        }
      }
    }
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShareAppMessage: function () {
  },
  onShow: function () {
    this.setime();
  },
  setime: function (options) {
    var that = this;
    var timer; // 计时器
    timer = setTimeout(function () {
      that.getreclist();
    }, 8000);
  },
  //获取消息条数
  getmessagelist: function (options) {
    var that = this;
    var openid = app.globalData.openid;
    var link = app.globalData.schlink;
    var uid = app.globalData.bindInfo.user_id;
    var sign = app.globalData.bindInfo.sign;
    wx.request({
      url: getApp().globalData.baseUrl + 'newcount',
      data: {
        link: link,
        openid: openid,
        sign: sign,
        uid: uid,
      },
      method: "get",
      header: {},
      success: function (res) {
        if (res.data.data.code==1){
          var text = res.data.data.data.count;
          if(text >0){
              text = String(text);
              wx.setTabBarBadge({
                index: 2,
                text: text
            })
          }
        }
      }
    })
  },
  
  //获取识别记录 家长账号 需要传学生id
  getreclist: function(options) {
    var that = this;
    if (app.globalData.openid && app.globalData.bindInfo){
      var openid = app.globalData.openid;
      var link = app.globalData.schlink;
      if (app.globalData.bindInfo.user_type == 3) {
        var uid = app.globalData.bindInfo.stu_id;
      } else {
        var uid = 0;
        return false;
      }
      //如果是家长账号是有学生id 传入
      wx.request({
        url: getApp().globalData.baseUrl + 'getreclist',
        data: {
          openid: openid,
          link: link,
          personid: uid
        },
        method: "get",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          var id1="";
          var id2 = "";
          var time1="";
          var time2="";
          var image1path="";
          var image2path="";
          if (res.data.result == 200) {
            if (res.data.data.length > 0) {
              var resdata = res.data.data;
              if (resdata[0]){
                 image1path = resdata[0]['path'];
                 image1path = image1path.replace(/[\r\n]/g, "");
                 id1 = resdata[0]['id'];
                 time1 = resdata[0]['time'];
              }
              if (resdata[1]) {
                image2path = resdata[1]['path'];
                image2path = image2path.replace(/[\r\n]/g, "");
                id2 = resdata[1]['id'];
                time2 = resdata[1]['time'];
              }
              that.setData({
                id1:id1,
                id2:id2,
                time1: time1,
                time2: time2,
                image1path: image1path,
                image2path: image2path
              })
              if(id1 || id2){
                that.setData({
                  backshow: true
                })
              }
            }
            that.setime();
          }
        }
      })
    }
  },
  //获取轮播图片
  getfoundlist: function (options) {
    var that = this;
    var openid = app.globalData.openid;
    var link = app.globalData.schlink;
    wx.request({
      url: getApp().globalData.baseUrl + 'lostrecord',
      data: {
        openid: openid,
        link: link,
        page: 1,
        limit:3,
        cid:7
      },
      method: "get",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code==200){
          if(res.data.data.data.length>0){
            var imagedata = res.data.data.data;
          }
          that.setData({ imgUrls: imagedata });
        }
      }
    })
  },
  //查看详情
  seedetail: function (e) {
    var dataId = e.currentTarget.dataset.idx;
    wx.navigateTo({
      url: '../classcard/photodetail?id=' + dataId
    })
  },
})